<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2022-03-26 22:35:38
 */

namespace app\admin\controller;
use app\common\controller\Backend;

class AuthGroup extends Backend {

	function initialize() {
		parent::initialize();
		$this->saveAndGetConfigIdentify('', '角色组', '', 'authGroup');

		$this->authGroupDao = model('admin/AuthGroup');
		$this->authRuleDao = model('admin/AuthRule');
		$this->authAccessDao = model('admin/AuthAccess');
		$this->authCateAccessDao = model('admin/AuthCateAccess');
	}

	public function index() {
		$map = $parameter = array();

		$count = $this->authGroupDao->countSearch($map);
		$Page = new \org\util\PageBootstrap($count, config('app.per_page'), $parameter);
		$pageShow = $Page->show();
		list($list) = $this->authGroupDao->search($map, $Page->getLimit());
		//print_r($list);exit;
		$this->assign('count', $count);
		$this->assign('pageShow', $pageShow);
		$this->assign('list', $list);

		return $this->fetch();
	}

	public function listorders() {
		$result = parent::baseListorders($this->authGroupDao);
		if ($result !== false) {
			$this->success('排序更新成功');
		} else {
			$this->error('排序更新失败');
		}
	}

	public function add() {
		$id = input('param.id', '', 'intval');

		if ($id) {
			$info = $this->authGroupDao->get($id);
		} else {
			$info = [
				'state' => 1
			];
		}
		$this->assign('id', $id);
		$this->assign('info', $info);

		return $this->fetch();
	}

	public function doAdd() {
		$id = input('post.id', '', 'intval');
		$data = input('post.data/a', '', 'pwEscape');

		if ($id) {
			$info = $this->authGroupDao->get($id);
		}

		//验证数据
		//$this->validateData($data, 'ManageGroup.edit');

		if ($id) {
			$result = $this->authGroupDao->baseUpdateData($id, $data);
			if ($result !== false) {
				$this->success('保存成功', url('admin/authGroup/index'));
			} else {
				$this->error('保存失败'.showDbError($this->authGroupDao));
			}
		} else {
			$result = $this->authGroupDao->baseAddData($data);
			if ($result !== false) {
				$this->success('添加成功', url('admin/authGroup/index'));
			} else {
				$this->error('添加失败'.showDbError($this->authGroupDao));
			}
		}
	}
	
	public function doDelete() {
		$id = input('param.id', '', 'intval');

		if ($id) {
			$info = $this->authGroupDao->get($id);
		}

		$result = $this->authGroupDao->del($id);
		if ($result !== false) {
			$this->success('删除成功', url('admin/authGroup/index'));
		} else {
			$this->error('删除失败'.showDbError($this->authGroupDao));
		}
	}

	public function access() {
		$groupid = input('param.id', '', 'intval');

		if ($groupid) {
			$info = $this->authGroupDao->getInfo($groupid);
		}
		$this->assign('groupid', $groupid);

		//根据组别获取菜单的树
		list($menutree) = $this->authRuleDao->getNewMenuTree($groupid);
		$this->assign('menutree', $menutree);
		
		return $this->fetch();
	}

	public function doAccess() {
		$groupid = input('post.groupid', '', 'intval');

		$menu_auth = input('post.menu_auth/a', '', 'pwEscape'); //获取所有授权菜单

		//access权限处理
		$this->authAccessDao->where(array('groupid'=>$groupid))->delete();
		$this->authCateAccessDao->where(array('groupid'=>$groupid))->delete();

		$ruleList = $this->authRuleDao->where('id', 'in', $menu_auth)->select();
		$ruleList = $ruleList->toArray();

		$data = $dataCid = array();
		foreach ($ruleList as $key => $value) {
			$data[] = array(
				'groupid'   => $groupid,
				'm'         => $value['module'],
				'c'         => $value['controller'],
				'a'         => $value['action'],
			);
			$dataCid[$value['cid']] = $value['cid'];
		}
		$result = $this->authAccessDao->saveAll($data);

		//分类access权限处理
		$data = array();
		foreach ($dataCid as $key => $value) {
			$data[] = array(
				'groupid'   => $groupid,
				'cid'       => $value,
			);
		}
		$this->authCateAccessDao->saveAll($data);

		if ($result !== false) {
			$this->success('权限分配成功', url('admin/authGroup/index'));
		} else {
			$this->error($this->authAccessDao->getError());
		}
	}

}